﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using DoAnWeb.DTO;
using DoAnWeb.BUS;

namespace DoAnWeb
{
    public partial class Cart : System.Web.UI.Page
    {
        double m_TongTien = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CurrentContext.IsLogged)
                Response.Redirect("~/Default.aspx");
            if (!IsPostBack)
                ShowCart();
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvCartInfo.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    int MaTinKhuyenMai = Convert.ToInt32(gvCartInfo.DataKeys[row.RowIndex].Value);
                    int SoLuong = Convert.ToInt32((row.Cells[2].FindControl("txtSoLuong") as TextBox).Text);

                    CurrentContext.Cart.UpdateItem(MaTinKhuyenMai, SoLuong);
                }
            }
            ShowCart();
        }

        void ShowCart()
        {
            lbThongBao.Visible = false;
            pnGioHang.Visible = true;
            if (CurrentContext.Cart == null)
            {
                lbThongBao.Visible = true;
                pnGioHang.Visible = false;
            }
            else
            {
                if (CurrentContext.Cart.Items.Count <= 0)
                {
                    lbThongBao.Visible = true;
                    pnGioHang.Visible = false;
                    pnCapNhap.Visible = false;
                    pnTongTien.Visible = false;
                }
                else
                {
                    pnGioHang.Visible = false;
                    pnCapNhap.Visible = false;
                    pnTongTien.Visible = false;

                    gvCartInfo.DataSource = CurrentContext.Cart.Items;
                    gvCartInfo.DataBind();
                    (Master as Site1).CalculateCartItems();
                }
            }
        }

        protected void gvCartInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int MaTinKhuyenMai = Convert.ToInt32(gvCartInfo.DataKeys[e.Row.RowIndex].Value);
                TinKhuyenMaiDTO Tin = new TinKhuyenMaiBUS().GetFromId(MaTinKhuyenMai);
                e.Row.Cells[0].Text = Tin.TieuDe;
                e.Row.Cells[1].Text = string.Format("{0:#,#.##}", Tin.GiaBan);
                int SoLuong = Convert.ToInt32((e.Row.Cells[2].FindControl("txtSoLuong") as TextBox).Text);

                double ThanhTien = Tin.GiaBan * SoLuong;
                e.Row.Cells[3].Text = string.Format("{0:#,#.##}", ThanhTien);
                m_TongTien += ThanhTien;
            }
        }

        protected void gvCartInfo_DataBound(object sender, EventArgs e)
        {
            lbTongTien.Text = string.Format("{0:#,#.##}", m_TongTien) + " VNĐ.";
        }

        protected void gvCartInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int MaTinKhuyenMai = Convert.ToInt32(gvCartInfo.DataKeys[e.RowIndex].Value);
            CurrentContext.Cart.DeleteItem(MaTinKhuyenMai);
            ShowCart();
        }

        protected void btnThanhToan_Click(object sender, EventArgs e)
        {
            DonHangBUS BUS = new DonHangBUS();
            DonHangDTO DTO = new DonHangDTO();

            DTO.HoTen = txtHoTen.Text;
            DTO.DiaChi = txtDiaChi.Text;
            DTO.DienThoai = txtSoDienThoai.Text;
            DTO.LoiNhan = txtLoiNhan.Text;
            DTO.TenTaiKhoan = CurrentContext.UserName;
            if (BUS.Add(DTO))
            {
                CT_DonHangBUS ctBUS = new CT_DonHangBUS();
                foreach (CartItem item in CurrentContext.Cart.Items)
                {
                    CT_DonHangDTO ctDTO = new CT_DonHangDTO();
                    ctDTO.MaTinKhuyenMai = item.MaTinKhuyenMai;
                    ctDTO.SoLuong = item.SoLuong;
                    ctDTO.MaDonHang = BUS.GetMaxID();
                    ctBUS.Add(ctDTO);
                    CurrentContext.Cart.DeleteItem(item.MaTinKhuyenMai);
                }
            }
            txtHoTen.Text = "";
            txtDiaChi.Text = "";
            txtSoDienThoai.Text = "";
            txtLoiNhan.Text = "";
            ShowCart();
        }

        protected void gvCartInfo_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}